package com.qianchang.microfinance.view.mine.bill;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.MyBillBean;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MyBillAdapter extends BaseRecyclerAdapter<MyBillBean.Bean> {


    public static final String ENTITY_KEY = "MyBillBean_Bean";

    public MyBillAdapter(Context context) {
        super(context, BaseRecyclerAdapter.ONLY_FOOTER);
    }

    @Override
    protected MyViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_my_bill, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, final MyBillBean.Bean entity, int position) {
        MyViewHolder vh = (MyViewHolder) holder;
//        final MyBillBean.Bean entity = item.getPageResult().getList().get(position);
        vh.tvProjectNum.setText(entity.getProjectNum());
        vh.ollBorrowMoney.setTextContent(entity.getLoanAmount());
        vh.ollCurrentRepay.setTextContent(entity.getPaymentAmount());
        String deadLine = entity.getDeadline() + ("1".equals(entity.getUnit()) ? mContext.getString(R.string.one_months) : mContext.getString(R.string.day));
        vh.ollBorrowPeriods.setText(deadLine);//借款期限  0：天 1：月
        vh.ollRepayDate.setText(entity.getRepayDate());
//        "overdueDays": "-140",  //逾期天数   小于0 时 提前还款，，=0 正常还款  ；  大于0 逾期还款
        String strRepayStatus;
        if (entity.getOverdueDays() > 0) {
            strRepayStatus = mContext.getString(R.string.btn_repay_after);
            vh.tvRepayStatus.setTextColor(mContext.getResources().getColor(R.color.common_red_color));
            vh.tvRepayStatus.setBackgroundResource(R.drawable.shape_button_gradient_01);
            vh.tvRepayStatus.setVisibility(View.VISIBLE);
        } else if (entity.getOverdueDays() == 0) {
            strRepayStatus = mContext.getString(R.string.btn_repay_now);
            vh.tvRepayStatus.setTextColor(mContext.getResources().getColor(R.color.common_orange_color_light));//
            vh.tvRepayStatus.setBackgroundResource(R.drawable.shape_button_gradient_02);
            vh.tvRepayStatus.setVisibility(View.VISIBLE);
        } else {
            strRepayStatus = mContext.getString(R.string.btn_repay_pre);
            vh.tvRepayStatus.setVisibility(View.GONE);
//            注释，隐藏该逻辑 micoolWong
//            vh.tvRepayStatus.setTextColor(mContext.getResources().getColor(R.color.common_orange_color_light));
//            vh.tvRepayStatus.setBackgroundResource(R.drawable.shape_button_gradient_02);
        }
        vh.tvRepayStatus.setText(strRepayStatus);

        String periods;
        if ("0".equals(entity.getUnit())) {//"unit": "0" // 0：天 1：月
            periods = mContext.getString(R.string.periods) + entity.getPeriod() + "/" + "1"; //期数
        } else {//月
            periods = mContext.getString(R.string.periods) + entity.getPeriod() + "/" + entity.getDeadline(); //期数
        }
        vh.tvPeriods.setText(periods);

        vh.tvRepayStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(ENTITY_KEY, entity);// MyBillBean.Bean
                Log.i("TAG","申请还款entity.getOverdueDays() = "+entity.getOverdueDays());
                if (entity.getOverdueDays() < 0) {
                    ((BaseActivity) mContext).startActivity(PrePayBackActivity.class, bundle);//提前还款
                } else {

                    /***
                     *      2018.7.16 注释掉 下面
                     */
                    ((BaseActivity) mContext).startActivity(PayBackActivity.class, bundle);//立即还款/逾期还款
        /************************************************************************************************************************************************************************/
                    /***
                     * 2018.7.16新增修改成通联支付方式，下面
                     */
//                    ((BaseActivity) mContext).startActivity(TLPayActivity.class, bundle);       //立即还款/逾期还款

                }
//                ToastUtil.getInstant().show(mContext,"下个版本开放敬请期待");
            }
        });

        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**/
                Bundle bundle = new Bundle();
                bundle.putSerializable(ENTITY_KEY, entity);
                ((BaseActivity) mContext).startActivity(MyBillDetailActivity.class, bundle);
            }
        });
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_project_num)
        DTextView tvProjectNum;
        @BindView(R.id.tv_periods)
        DTextView tvPeriods;
        @BindView(R.id.oll_borrow_money)
        DTextView ollBorrowMoney;
        @BindView(R.id.oll_current_repay)
        DTextView ollCurrentRepay;
        @BindView(R.id.oll_borrow_periods)
        DTextView ollBorrowPeriods;
        @BindView(R.id.oll_repay_date)
        DTextView ollRepayDate;
        @BindView(R.id.tv_repay_status)
        DTextView tvRepayStatus;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
